#ifndef SI_H_
#define SI_H_

#include "LED.h"
#include "Relay.h"
#include "Button.h"
#include "DataType.h"

enum SI_ADC_STATE {
	SI_ADC_NORMAL,
	SI_ADC_FAULT,
	SI_ADC_ALARM,
	SI_ADC_NULL
};

#define TEST_SO_FAULT_SEQ 0
#define EXP_CFG 0
#define GLOC_CFG 1

#define SI_ACTIVE 8

enum SI_STATE { /* DO NOT CHANGE ORDER OF STATES!!! */
	SI_STATE_IDLE,
	SI_STATE_alarmEntryTimer,
	SI_STATE_alarmEntryTimerFWS_waitForAck,
	SI_STATE_alarmEntryTimerFWS_Acked,
	SI_STATE_alarmWaitForAck,
	SI_STATE_alarmWaitForResetOrFltClear,
	SI_STATE_alarmWaitForAck_afterResetOrFltClear,
	SI_STATE_faultWaitForAck,
	SI_STATE_faultWaitForFltClear,
	SI_STATE_faultWaitForAck_afterFltClear,

//	SI_STATE_NS_IDLE,
	SI_STATE_NS_begin,
	SI_STATE_NS_smokeCheck_waitForAck = SI_STATE_NS_begin,
	SI_STATE_NS_smokeCheck_Acked,
	SI_STATE_NS_alarmWaitForAck,
	SI_STATE_NS_alarmWaitForReset,
	SI_STATE_NS_resetOn,
	SI_STATE_NS_resetOff,
	SI_STATE_NS_clearTimer,
	SI_STATE_NS_confirmTimer,
	SI_STATE_NS_alarmWaitForAck_afterResetAndAlmClear,
	SI_STATE_NS_last = SI_STATE_NS_alarmWaitForAck_afterResetAndAlmClear
};
enum SI_X {
	SI_1=0,
	SI_2=1,
	SI_3=2,
	SI_4=3,
	SI_5=4,
	SI_6=5,
	SI_7=6,
	SI_8=7,
	SI_9=8,
	SI_10=9,
	SI_11=10,
	SI_12=11,
	SI_NULL = 12
};
#define SI_1_to_8 0xFF
enum SI_X_mask {
	SI_1m=0x01,
	SI_2m=0x02,
	SI_3m=0x04,
	SI_4m=0x08,
	SI_5m=0x10,
	SI_6m=0x20,
	SI_7m=0x40,
	SI_8m=0x80
};
enum SI_runMode {
	SI_runMode_norm = 0,
	SI_runMode_test = 1
};
#define TIME_CLEAR_OFF 0
#define TIME_CLEAR_OFF_10 10
#define TIME_CONFIRM_OFF 0
#define TIME_CONFIRM_OFF_30 30
#define TIME_SMOKE_OFF 0
#define TIME_SMOKE_OFF_10 10
#define TIME_SMOKE_OFF_30 30
#define TIME_ROFF_OFF 0
#define TIME_RON_OFF 0
#define TIME_ROFF_OFF_5 5
#define TIME_RON_OFF_5 5
#define TIME_AlmEntry_0 0
#define TIME_AlmEntry_5 5
#define TIME_AlmExit_5 5
#define TIME_AlmExit_0 0
#define SI_ADC_FILTER_COUNT 4
struct SI_threadInfo_t {
	enum SI_ADC_STATE adcState;
	enum SI_ADC_STATE adcStateTarget;
	unsigned char adcFilterCount; // for simple adc filter, count to SI_ADC_FILTER_COUNT before transition
	enum SI_STATE alarmState;
	enum SI_STATE faultState;
	enum LED_INDEX alarmLed;
	enum LED_INDEX faultLed;
	enum LED_INDEX globalAlarmLed;
	enum LED_INDEX globalFaultLed;
	unsigned char buzzerValid;
	unsigned int adcCount;
	unsigned int oneSecTickCount;
	unsigned char isAcked;
	unsigned char inReset;
	struct SI * pSI;
};
struct SI_threadInfo_t * SI_rtnThreadInfo(enum SI_X siMe);
void SI_init (void);
int  SI_24VOutput_CheckAndDrive(void);
void SI_thread(unsigned char setBreakpoint, enum SI_runMode adcRawMode, enum SI_runMode adcStatusMode);
void SI_init25msTimerISR(void);
void SI_enable25msISR(void);
void SI_disable25msISR(void);
void SI_atomicTimer(void);
enum SI_ADC_STATE SI_getAdcState(enum SI_X siMe);

void SI_relayHelper(enum LED_INDEX almLed, enum LED_INDEX globalAlarmLed, enum Relay_Setting relaySetting);

enum SI_testCfg {
	SI_testCfg_DontUse,
	SI_testCfg_use
};
void SI_24VOutput_test2(void);
void SI_24VOutput_set(enum SI_X_mask si);
void SI_24VOutput_clear(enum SI_X_mask si);

void SI_threadInfo_setGlobalLed(struct SI_threadInfo_t * me, enum LED_INDEX gblAlm);
void SI_threadInfo_reset(struct SI_threadInfo_t * me);

void SI_almShortingType_SM(unsigned char i, struct evtArr_s * evts, unsigned char eventOneSecondTick);

extern const struct configMemRAM_t * pConfigMemRAM;

#endif /* SI_H_ */
